package com.beauty.product.repository;

import com.beauty.product.entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ProductRepository extends JpaRepository<Product, String>, JpaSpecificationExecutor<Product> {
    
    Optional<Product> findBySku(String sku);
    
    Page<Product> findByCategoryId(Long categoryId, Pageable pageable);
    
    Page<Product> findByIsPublished(Boolean isPublished, Pageable pageable);
    
    Page<Product> findByIsRecommended(Boolean isRecommended, Pageable pageable);
    
    Page<Product> findByIsNew(Boolean isNew, Pageable pageable);
    
    Page<Product> findByIsHot(Boolean isHot, Pageable pageable);
    
    List<Product> findTop10ByOrderBySalesDesc();
    
    Page<Product> findByNameContaining(String keyword, Pageable pageable);
    
    Page<Product> findBySkuContaining(String keyword, Pageable pageable);
    
    Page<Product> findByBrandContaining(String keyword, Pageable pageable);
    
    Page<Product> findByDescriptionContaining(String keyword, Pageable pageable);
    
    /**
     * 查询所有以PRD开头的商品ID列表
     * @return 所有以PRD开头的商品ID列表
     */
    @Query("SELECT p.id FROM Product p WHERE p.id LIKE 'PRD%'")
    List<String> findAllProductIdsStartingWithPRD();
    
    /**
     * 查询最大的PRD格式商品ID
     * @return 最大的PRD格式商品ID
     */
    @Query("SELECT MAX(p.id) FROM Product p WHERE p.id LIKE 'PRD%'")
    String findMaxProductId();
    
    @Query("SELECT p FROM Product p WHERE " +
           "LOWER(p.name) LIKE LOWER(CONCAT('%', ?1, '%')) OR " +
           "LOWER(p.sku) LIKE LOWER(CONCAT('%', ?1, '%')) OR " +
           "LOWER(p.brand) LIKE LOWER(CONCAT('%', ?1, '%')) OR " +
           "LOWER(p.description) LIKE LOWER(CONCAT('%', ?1, '%'))")
    Page<Product> searchByKeyword(String keyword, Pageable pageable);
} 